class User < ActiveRecord::Base
  has_many :posts, :dependent => :destroy
  has_many :comments, :dependent => :destroy
  named_scope :recent_visited_users, :order => "last_request_at DESC",:limit => 20

  # =>将错误信息汉化
  self.merge_validates_format_of_email_field_options({:message => I18n.t('error_messages.email_invalid', :default => '邮箱地址格式不正确（example@example.com）')})# 邮箱 
  self.merge_validates_length_of_email_field_options({:message => I18n.t('error_messages.email_invalid', :default => '邮箱地址长度不正确(6到100个字)')}) 
  self.merge_validates_uniqueness_of_email_field_options({:message => I18n.t('error_messages.email_invalid', :default => '邮箱地址重复，请更换')}) 
  self.merge_validates_format_of_login_field_options({:message => I18n.t('error_messages.login_invalid', :default => '用户名格式不对，只允许包含字符、数字、空格和.-_@')})#用户名 
  self.merge_validates_length_of_login_field_options({:message => I18n.t('error_messages.login_invalid', :default => '用户名长度不正确（3到100个字）')}) 
  self.merge_validates_uniqueness_of_email_field_options({:message => I18n.t('error_messages.login_invalid', :default => '用户名重复，请更换）')}) 
  self.merge_validates_confirmation_of_password_field_options({:message => I18n.t('error_messages.password_invalid', :default => '两次输入的密码不一致')})#密码 
  self.merge_validates_length_of_password_confirmation_field_options({:message => I18n.t('error_messages.password_invalid', :default => '第二次输入的密码长度太短（不能小于4个字）')}) 
  self.merge_validates_length_of_password_field_options({:message => I18n.t('error_messages.password_invalid', :default => '密码长度太短（不能小于4个字）')})
  acts_as_authentic 
  USERNAME_MINIMUM = 3 # 用户名最短长度
  USERNAME_MAXIMUM = 100 # 用户名最长长度
  PASSWORD_MINIMUM = 4 # 密码最短长度
  validates_format_of :website,
    :with => /^https?:\/\/([A-Z0-9]{1,}\.)?([A-Z0-9]{1,})(\.[A-Z]{2,4}){1,2}$/i,
    :message => "网站地址格式不正确"
  # => 头像
  def avatar
    "http://www.gravatar.com/avatar/#{Comment.email_to_md5(email)}?s=40"
  end

end
